package com.software.loginregister.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.software.loginregister.R;
import com.software.loginregister.entity.Works;
import com.software.loginregister.entity.vo.UserFollowDTO;

import java.util.ArrayList;
import java.util.List;

public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.ViewHolder> {
    private List<UserFollowDTO> users = new ArrayList<>();
     // 修改默认图片资源为你的橙色小图标
    private static final int DEFAULT_IMAGE = R.drawable.default_image;
    private static final int AVATAR_DEFAULT_IMAGE = R.drawable.ic_default_avatar;

    public void setData(List<UserFollowDTO> users) {
        this.users = users;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserFollowDTO user = users.get(position);
        
         // 加载头像
        loadAvatar(holder.avatarImage, user.getAvatar());

        // 设置用户信息
        holder.usernameText.setText(user.getNickname());
        holder.followersCountText.setText(String.valueOf(user.getFollowersCount()));
        holder.worksCountText.setText(String.valueOf(user.getWorksCount()));

        // 加载作品图片
        List<Works> topWorks = user.getTopWorks();

        // 处理第一张图片
        loadWorkImage(holder.workImage1, topWorks, 0);

        // 处理第二张图片
        loadWorkImage(holder.workImage2, topWorks, 1);

        // 处理第三张图片
        loadWorkImage(holder.workImage3, topWorks, 2);
    }

     // 加载头像的方法
    private void loadAvatar(ImageView avatarImage, String avatarUrl) {
        if (!TextUtils.isEmpty(avatarUrl)) {
            Glide.with(avatarImage.getContext())
                .load(avatarUrl)
               .placeholder(AVATAR_DEFAULT_IMAGE)  // 加载时显示默认图片
                .error(R.drawable.image_error)        // 加载失败时显示默认图片
                .circleCrop()  // 使用圆形裁剪
                .into(avatarImage);
        } else {
            Glide.with(avatarImage.getContext())
                .load(R.drawable.image_error)
                .circleCrop()
                .into(avatarImage);
        }
    }
    private void loadWorkImage(ImageView imageView, List<Works> works, int index) {
        if (works != null && works.size() > index && works.get(index) != null
            && !TextUtils.isEmpty(works.get(index).getPhotoUrl())) {
            // 有图片时加载网络图片
            Glide.with(imageView.getContext())
                .load(works.get(index).getPhotoUrl())
                .placeholder(DEFAULT_IMAGE)  // 加载时显示默认图片
                .error(R.drawable.image_error)        // 加载失败时显示默认图片
                .centerCrop()
                .into(imageView);
        } else {
            Log.d("loadWorkImageDebug", "No image found for index: " + index);

            // 没有图片时显示默认图片
            Glide.with(imageView.getContext())
                .load(DEFAULT_IMAGE)
                .centerCrop()
                .into(imageView);
        }
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ShapeableImageView avatarImage;
        private final TextView worksCountText;
        TextView usernameText;
        TextView followersCountText;
        ImageView workImage1;
        ImageView workImage2;
        ImageView workImage3;

        ViewHolder(View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.siv_item_follow_avater);
            usernameText = itemView.findViewById(R.id.tv_item_follow_username);
            followersCountText = itemView.findViewById(R.id.tv_item_follow_follower_count);
            worksCountText = itemView.findViewById(R.id.tv_item_follow_works_count);
            workImage1 = itemView.findViewById(R.id.tv_item_follow_work1);
            workImage2 = itemView.findViewById(R.id.tv_item_follow_work2);
            workImage3 = itemView.findViewById(R.id.tv_item_follow_work3);
        }
    }
}
